<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

use App\Model\Company;
use App\Model\Checknegative;

use DB;

class Comcheck extends Model
{
    //
    protected $table = 'comcheck';

	public $timestamps = false;
    public function company()
    {
        return $this->belongsTo('App\Model\Company','companyId');
    }

    public function users()
    {
        return $this->belongsTo('App\User','userId');
    }
    public function Comcheckdetail()
    {
        return $this->hasMany('App\Model\Comcheckdetail','checkId','id')->where(['comcheckdetail.status'=>2]);
    }
    public function Comcheckagain()
    {
        return $this->hasMany('App\Model\Comcheckagain','checkId','id')->where(['comcheckagain.status'=>2]);
    }
    public function getCompany()
    {
    	return $this->belongsTo('App\Model\Company','companyId');
    }

    public function getStandardNegative($componyId)
    {
    	$details = DB::select("select detail.* from comcheckdetail detail 
    				left join comcheck on detail.checkId = comcheck.id
    				where comcheck.companyId = $componyId and detail.status = 2 and comcheck.status = 1 ");
    	$negativeStr = '';
    	foreach($details as $k => $v)
    	{
            if($v->negatives) {
                $negativeStr .= $v->negatives;
            }
    		
    	}
        $negative = explode(',', substr($negativeStr, 0, -1));
    	$rs = Checknegative::wherein('id',$negative)->get();
    	return $rs;
    }

    public function getProductNegative($componyId)
    {
    	$details = DB::select("select detail.* from comcheckdetail detail 
    				left join comcheck on detail.checkId = comcheck.id
    				where comcheck.companyId = $componyId and detail.status = 2 and comcheck.status = 2 ");
    	$negativeStr = '';
    	foreach($details as $k => $v)
    	{
            if($v->negatives) {
                $negativeStr .= $v->negatives;
            }
    		
    	}
        $negative = explode(',', substr($negativeStr, 0, -1));
    	$rs = Checknegative::wherein('id',$negative)->get();
    	return $rs;
    }

    public function getChecks($com_name) 
    {
        $where = "1=1";
        if(!empty($com_name)) {
            $where .= " and company.comName like '%".$com_name."%'";
        }
        $checks = DB::select("select comcheck.id as checkId,comcheck.created,comcheck.updated,comcheck.wordsUrl,company.id,company.comName from comcheck 
                    left join company on comcheck.companyId = company.id
                    where ".$where);
        return $checks;
    }

    public function getCheckss($com_name,$district_id,$street_id,$community_id,$category_id=0)
    {
        $where = "1=1";
        if(!empty($com_name)) {
            $where .= " and company.comName like '%".$com_name."%' ";
        }
        if(!empty($district_id)) {
            $where .= " and company.district = ".$district_id;
        }
        if(!empty($street_id)) {
            $where .= " and company.street = ".$street_id;
        }
        if(!empty($community_id)) {
            $where .= " and company.community = ".$community_id;
        }
        if(!empty($category_id)) {
            $where .= " and company.categoryId = ".$category_id;
        }
        $checks = DB::select("select comcheck.id as checkId,comcheck.created,comcheck.updated,comcheck.wordsUrl,company.id,company.comName,company.district,company.street,company.community from comcheck 
                    left join company on comcheck.companyId = company.id
                    where ".$where);
        return $checks;
    }

}
